/*
	So, this is only a quick demo of what Cappuccino can do,
	coded up in about 2 hours total, with helpful input from
	the 280North guys (Ross Boucher in particular).
	
	Hope it's of some use to anyone wanting to check out
	Cappuccino for some real work =).
	
	Created by Steven Troughton-Smith, 28 September 08
	Artwork © Adam Betts, do not reuse without permission
*/


import <Foundation/CPObject.j>
import <AppKit/CPView.j>
import <AppKit/CPButton.j>
import <AppKit/CPWebView.j>
import "LOBoard.j"

function CPCurrentGraphicsPort()
{
	return [[CPGraphicsContext currentContext] graphicsPort];
}

@implementation LOInfoView : CPView
{
}

-(void)drawRect:(CGRect)r
{
	CGContextSetFillColor(CPCurrentGraphicsPort(), [CPColor whiteColor]);
	CGContextFillRoundedRectangleInRect(CPCurrentGraphicsPort(), CGRectMake([self bounds].origin.x+5,[self bounds].origin.y , 295, 395), 10, YES, YES, YES, YES);
}
@end

@implementation AppController : CPObject
{
	CPWindow rootWindow;
	CPPanel infoWindow;
	CPPanel gameWindow;

}

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
	/* Enable Logging (only use if debugging) */
	// CPLogRegister(CPLogPopup);

    rootWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
	[rootWindow setBackgroundColor:[CPColor grayColor]];
	[rootWindow orderFront:self];

	/* Marketing blurb - gotta love it =) */
	[self initInfoWindow];
    [infoWindow orderFront:self];
	
	/* This gets the important stuff working */
	[self initGameWindow];  
    [gameWindow orderFront:self];
}

-(void)initInfoWindow
{
	infoWindow = [[CPPanel alloc] initWithContentRect:CGRectMake(400,50,320,520)styleMask:CPHUDBackgroundWindowMask|CPResizableWindowMask];
	[infoWindow setFloatingPanel:YES];
	var _infoContent = [infoWindow contentView];
	
	var _iconImage = [[CPImage alloc] initWithContentsOfFile:"Resources/icon.png" size:CPSizeMake(59, 60)];
	var _iconView = [[CPImageView alloc] initWithFrame:CGRectMake(125,0,59,60)];

	[_iconView setImage:_iconImage];
	
	[_infoContent addSubview:_iconView];
	
	var _infoView = [[LOInfoView alloc] initWithFrame:CGRectMake(0,65,320,395)];
	var _webView = [[CPWebView alloc] initWithFrame:CGRectMake(20,0,270,370)];
	
	[_webView loadHTMLString:@"<center><h3>Lights Off</h3></center> <p>Lights Off is a fantastic game exclusively for iPhone and iPod touch and inspired by Tiger Electronic's 'Lights Out'.</p> <p>The goal of the game is simply to switch all of the lights off, but it's harder than it looks! Give the first few levels a try in the playable demo to the left.</p><center><img src='Resources/avail_on_app_store.png'></center>"];
	
	[_infoView addSubview:_webView];
	
	[_infoContent addSubview:_infoView];
}

-(void)initGameWindow
{
 	gameWindow = [[CPPanel alloc] initWithContentRect:CGRectMake(0,50,335,520) styleMask:CPHUDBackgroundWindowMask];
	[gameWindow setFloatingPanel:YES];
	[gameWindow setTitle:@"Lights Off"];
	
	contentView = [gameWindow contentView];

	var _board = [[LOBoard alloc] initWithFrame:CGRectMake(0,0,320,480)];

	var _bgImage = [[CPImage alloc] initWithContentsOfFile:"Resources/lo-background.png" size:CPSizeMake(320, 480)];
	[_board setImage:_bgImage];
	[_board resetBoard];
	
	var _buttonImage = [[CPImage alloc] initWithContentsOfFile:"Resources/button-reset.png" size:CPSizeMake(90, 28)];
	var _buttonPressImage = [[CPImage alloc] initWithContentsOfFile:"Resources/button-reset-press.png" size:CPSizeMake(90, 28)];

	var _resetButton = [[CPButton alloc] initWithFrame:CGRectMake(195,422,90,28)];
	[_resetButton setImage:_buttonImage];
	[_resetButton setAlternateImage:_buttonPressImage];
	[_resetButton setBordered:NO];
	
    [contentView addSubview:_board];
	[contentView addSubview:_resetButton];
	
	[_resetButton setTarget:_board];
	[_resetButton setAction:@selector(resetBoard)];
}

@end